//set head navbar width
function setNavbarWidth(group,width){
	var ul=document.createElement("ul");
	var li="";
	ul.className="navbar";
	for(var i=0;i<group.length;i++){	
		if(i==0){
			li=document.createElement("li");
			li.style.width=width;
			li.setAttribute("url",group[i].url);
		    li.className="btn-active";
		    li.innerHTML=group[0].caption;
		    $(li).bind('click', function(){
		    	 zb.global.list_url = $(this).attr("url");
		    	 refreshList(zb.global.list_url,$(this),'');
		    });
		    ul.appendChild(li);
		}else{
			li=document.createElement("li");
			li.style.width=width;
			li.setAttribute("url",group[i].url);
		    li.innerHTML=group[i].caption;
		    $(li).bind('click', function(){
		    	 zb.global.list_url = $(this).attr("url");
		    	 refreshList(zb.global.list_url,$(this),'');
		    });
		    ul.appendChild(li);
		}		
	}
	return ul;
}
//refresh list
function refreshList(url,obj,type){
    $(obj).addClass("btn-active").siblings("li").removeClass("btn-active");
    zb.submit(url,'report' , 'init', 'refresh' , '' , zb.global.menu.header.session, type,'list'); 
    loadList();   
}

//load navbar
function createNavbar(){
	var report=zb.global.data.body.report;
	var el=document.getElementById("navbar");
	var ul="";
		$(el).empty();
		if(report.group.length==2){
			ul=setNavbarWidth(report.group,'50%');
		}
		else if(report.group.length==3){
			ul=setNavbarWidth(report.group,'33.30%');
		}
		else if(report.group.length==4){
			ul=setNavbarWidth(report.group,'25%');
		}
		else if(report.group.length>5){
			ul=setNavbarWidth(report.group,'100px');
		}
		el.appendChild(ul);	
		initSearch(report);
		zb.global.list_url=zb.global.data.body.report.group[0].url;
	    refreshList(zb.global.data.body.report.group[0].url,'route','');
}

function initSearch(report){
	 $("#search").empty()
	 if(report.hasOwnProperty('search')){
		 var table = document.createElement("table");
		 var el=document.getElementById("search");
		 var tr	=document.createElement("tr");
		 var td_1=document.createElement("td");
		 var td_2=document.createElement("td");
		 var inp=document.createElement("input");
		 var btn=document.createElement("button");
		 	 table.className="search";
		 	 inp.className="inp";
		  	 btn.innerHTML="检索";
		 	$(btn).bind('click', function(){
		 			zb.global.id=report.search.keybox.id;
		 			zb.global.val=inp.value;
		 			refreshList(zb.global.list_url,'route','loadlist');
		 	 });
		  	 td_1.appendChild(inp);
		     td_2.width='56';
		     td_2.appendChild(btn);
		     tr.appendChild(td_1);
		     tr.appendChild(td_2);
		     table.appendChild(tr);
		     el.appendChild(table);
		 
	 }
}


// set list templete
function fieldController(tb_ui){
 var config= new Array();
	switch(tb_ui){
		case "report.list.notice":
			config[0] = {idx:1,colspan:false,color:"#000",align:"left"};
			config[1] = {idx:1,colspan:false,color:"#FF0000",align:"right"};
			config[2] = {idx:2,colspan:true,color:"#000",align:"left"};
			config[3] = {idx:2,colspan:false,color:"#000",align:"left"};
			config[4] = {idx:4,colspan:false,color:"#000",align:"left"};
			config[5] = {idx:5,colspan:true,color:"#000",align:"left"};
			config[6] = {idx:6,colspan:false,color:"#000",align:"left"};
			break
		case "report.list.tel":
			config[0] = {idx:1,colspan:false,color:"#000",align:"left"};
			config[1] = {idx:1,colspan:false,color:"#FF0000",align:"right"};
			config[2] = {idx:3,colspan:false,color:"#000",align:"left"};
			config[3] = {idx:3,colspan:false,color:"#000",align:"left"};
			config[4] = {idx:4,colspan:true,color:"#000",align:"left"};
			config[5] = {idx:2,colspan:false,color:"#000",align:"left"};
			config[6] = {idx:6,colspan:false,color:"#059",align:"left"};
			config[7] = {idx:7,colspan:true,color:"#000",align:"left"};
			break
		case "report.list.teltop":
			config[0] = {idx:1,colspan:false,color:"#000",align:"left"};
			config[1] = {idx:1,colspan:false,color:"#FF0000",align:"right"};
			config[2] = {idx:3,colspan:false,color:"#000",align:"left"};
			config[3] = {idx:3,colspan:false,color:"#000",align:"left"};
			config[4] = {idx:3,colspan:false,color:"#000",align:"left"};
			config[5] = {idx:5,colspan:false,color:"#000",align:"left"};
			config[6] = {idx:6,colspan:true,color:"#059",align:"left"};
			config[7] = {idx:7,colspan:true,color:"#000",align:"left"};
			config[8] = {idx:4,colspan:false,color:"#000",align:"left"};
			config[9] = {idx:9,colspan:false,color:"#000",align:"left"};
			config[10] = {idx:10,colspan:true,color:"#059",align:"left"};
			break
		 default :
            
	}
	return config;
}

//show list field
function setTable(tb_ui , col){
    var row=fieldController(tb_ui);
	var html = "<tr>";
	for(var i = 0 ;i<col.length ; i ++){
		var text="";
		for(var key in col[i]){
			if(key!="undefined"){
				text=key+":"+col[i][key];
			}
			else{
				text=col[i][key];
			}
		} 
		html += "<td align='"+row[i].align+"' style='color:"+row[i].color+"' " +(row[i].colspan ? "colspan = '2'"  :"" ) +">"+text+"</td>";
		if (i== row[i].idx ){
			html += "</tr>";
			if (i<col.length-1){
				html += "<tr>";
			}
		}     
	}
	return html ;
}

//load list
function loadList(){
	  var html="";
	  var table = zb.global.data.body.source.table;
	  var layout = table.layout;
	  var rows = table.rows;
  	  var cols = table.cols;
   	  var fields =layout.fields; 
		  for(var n=0;n<rows.length;n++){  
			  html+="<table class='table tb_3'>";
			  var col=new Array();
			  for(var k=0;k<fields.length;k++){
				   var idx = 0 ,fieldObj={};
				   for(var m=0;m<cols.length;m++){					  
					   if (fields[k].text == "{@"+cols[m].id+"}"){	idx = m;}					   
				   } 
			   	   fieldObj[fields[k].caption]=rows[n][idx];
			   	   
			       col[k]=fieldObj;
			       
			  }	
			  html += setTable(table.layout.uitype, col)+"</table>";
		  }
		  $("#module").empty().append(html);	
		  contentPages(table);
   	  }
//list fen page
function contentPages(table){
	var el=document.getElementById("pages");
        $(el).empty();
		for(var i=0;i<5;i++){
			var btn=document.createElement("button");
			btn.setAttribute("data-role","button");
			btn.setAttribute("data-inline","true");
			btn.setAttribute("data-mini","true");
			var currpage = 1 ;
			var maxpage=table.page.pagecount;
            switch(i){
            	case 0:
            		btn.innerHTML="首页";
            		btn.setAttribute("pageType","hompage");
            		break;
            	case 1:
            		btn.innerHTML="上一页";
            		btn.setAttribute("pageType","prepage");
            		break;
            	case 2:
            		//btn.innerHTML=currpage+"/"+topage;
            		btn.setAttribute("pageType","gotopage");
            		btn.innerHTML="<span id='pageSize'>"+currpage+"</span>/"+maxpage;
            		break;
            	case 3:
            		btn.innerHTML="下一页";
            		btn.setAttribute("pageType","nextpage");
            		
            		break;
            	case 4:
            		btn.innerHTML="尾页";
            		btn.setAttribute("pageType","endpage");

            		break;
            }
            $(btn).bind('click', function(){
    			var pageType=$(this).attr("pageType");
        			
    			if(pageType=="hompage"){
    				currpage = 1;
    			}
    			else if(pageType=="prepage"){
    				currpage = currpage == 1 ? 1 : currpage - 1;
    				
    			}
    			else if(pageType=="gotopage"){
    				
    			}
    			else if(pageType=="nextpage"){
    				currpage = currpage<maxpage ?  currpage + 1 : maxpage;
    			}
    			else if(pageType=="endpage"){
    				currpage = maxpage ;
    			}
    			zb.global.id="pageindex";
    			zb.global.val=currpage;
    			refreshList(zb.global.list_url,'route','loadlist');
    			document.getElementById("pageSize").innerHTML=currpage
    		});
			el.appendChild(btn);
		}
}




